package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.entity.AdvancedTraining;
import com.example.teesystem.entity.AdvancedTrainingProgress;
import com.example.teesystem.entity.vo.advancedTrain.AdAnswer;
import com.example.teesystem.entity.vo.advancedTrain.AdResponse;
import com.example.teesystem.entity.vo.advancedTrain.AdTrainProgressUploadVo;
import com.example.teesystem.entityDto.IssueSimpleAnswer;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.AdvancedTrainingMapper;
import com.example.teesystem.service.AdvancedTrainingProgressService;
import com.example.teesystem.mapper.AdvancedTrainingProgressMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author 28131
* @description 针对表【t_advanced_training_progress】的数据库操作Service实现
* @createDate 2022-04-02 11:05:55
*/
@Service
@RequiredArgsConstructor
public class AdvancedTrainingProgressServiceImpl extends ServiceImpl<AdvancedTrainingProgressMapper, AdvancedTrainingProgress>
    implements AdvancedTrainingProgressService{

    private final IssueServiceImpl issueService;
    private final AdvancedTrainingProgressMapper advancedTrainingProgressMapper;
    private final AdvancedTrainingMapper advancedTrainingMapper;

    @Override
    public boolean add(AdTrainProgressUploadVo adTrainProgressUploadVo) {
        AdvancedTrainingProgress advancedTrainingProgress = advancedTrainingProgressMapper.getAdvancedTraining(adTrainProgressUploadVo.getAdvancedTrainingId(),adTrainProgressUploadVo.getStudentId());
        if(advancedTrainingProgress!=null){
            return true;
        }
        return this.save(new AdvancedTrainingProgress(adTrainProgressUploadVo));
    }
    
    @Override
    public List<AdvancedTrainingProgress> get(Integer studentId) {
        QueryWrapper<AdvancedTrainingProgress> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", studentId);
        System.out.println(this.list(wrapper));
        return this.list(wrapper);
    }
    
    @Override
    public List<AdvancedTrainingProgress> get(List<Integer> studentIds) {
        QueryWrapper<AdvancedTrainingProgress> wrapper = new QueryWrapper<>();
        wrapper.in("student_id", studentIds);
        return this.list(wrapper);
    }

    @Override
    public Response answer(AdAnswer adAnswer) {
        AdvancedTraining advancedTraining = advancedTrainingMapper.getById(adAnswer.getAdvancedTrainId());
        List<IssueSimpleAnswer> issueSimpleAnswers = new ArrayList<>();
        if(adAnswer.getAnswers().size()==0||adAnswer.getAnswers()==null){
            return Response.ok();
        }
        int count = 0;
        for(int i = 0; i < adAnswer.getAnswers().size(); i++){
            issueSimpleAnswers.add(issueService.checkAnswer(adAnswer.getAnswers().get(i)));
        }
        for (IssueSimpleAnswer issueSimpleAnswer : issueSimpleAnswers) {
            if (issueSimpleAnswer.getIsRight()) {
                count++;
            }
        }
        AdResponse adResponse = new AdResponse();
        adResponse.setIssueSimpleAnswers(issueSimpleAnswers);
        adResponse.setRightNum(count);
        if(count/issueSimpleAnswers.size()>=0.8){
            this.add(new AdTrainProgressUploadVo(MyHandlerInterceptor.getUid(),adAnswer.getAdvancedTrainId(),advancedTraining.getCourseId()));
            adResponse.setPass(true);
        }

        return Response.ok(adResponse);
    }

    @Override
    public Response getFinishAndOpenId(int courseId) {
        List<AdvancedTraining> advancedTrainings = advancedTrainingMapper.getAdvancedTrainingByCourseId(courseId);
        List<Integer> advancedTrainingIntegers = new ArrayList<>();
        Map<String,Set<Integer>> resultMap = new HashMap<>();
        List<AdvancedTrainingProgress> advancedTrainingProgresses = null;
        Set<Integer> result = new HashSet<>();

        if(advancedTrainings!=null && advancedTrainings.size()!=0){
            for(AdvancedTraining advancedTraining : advancedTrainings){
                advancedTrainingIntegers.add(advancedTraining.getId());
            }
            advancedTrainingProgresses = advancedTrainingProgressMapper.getAdvancedTrainingProgressesByIds(advancedTrainingIntegers,MyHandlerInterceptor.getUid());
            for(AdvancedTraining advancedTraining : advancedTrainings){
                if(advancedTraining.getIsOpen() == 1) {
                    result.add(advancedTraining.getId());
                }
            }
        }

        if(advancedTrainingProgresses.size() != 0 && advancedTrainingProgresses!=null){
            for(AdvancedTrainingProgress advancedTrainingProgress : advancedTrainingProgresses){
                result.add(advancedTrainingProgress.getAdvancedTrainingId());
            }
        }
        resultMap.put("result",result);
        return Response.ok(resultMap);
    }
}




